<?php
/**
 * Created by Mauro Gadaleta
 * User: Mauro Gadaleta
 * Date: 28/04/11
 * Time: 12:22
 *
 * Gadget Recordset
 */
class recordsetCMS extends gadgetsCMS {

    /**
     * @var array
     */
	private $data = array();

    /**
     * @var array
     */
	private $actions = array();

    /**
     * @var string
     */
	private $idName = "";

    /**
     * @var array
     */
	private $booleanColumn = array();

    /**
     * @var array
     */
	private $actionsAll = array();

    /**
     * @var string
     */
	public $template = 'recordset.tpl';

    /**
     * Constructor set up
     * @param array $array
     */
	function __construct($array = array()) {
		parent::__construct();
		
		$this->data = $array;
		$this->setHeaders();
		$this->setContent();
		$this->setId();
	}

    /**
     * Setea el contenido 
     * @return void
     */
	private function setContent() { $this->assign('content',$this->data); }

    /**
     * Setea los headers
     * @return void
     */
	private function setHeaders() {
		$headers = array();
		if(!empty($this->data))
		{
			foreach($this->data[0] as $header => $value)
			{
				if($header == 'id')
					$this->idName = $header;
				else
					$headers[] = $header;
			}
			$this->assign('headers',$headers);
		}
	}

    /**
     * Devuelve el gadget
     * @return string
     */
	public function get() {
		$this->setActions();
		$this->assign('booleanColumns',$this->booleanColumn);
		$this->assign('actionsAll',$this->actionsAll);
		
		return parent::get($this->template);
	}

    /**
     * Setea las acciones de la tabla
     * @param  $action
     * @param null $target
     * @return void
     */
	public function setAction($action,$target=null) {
		$this->actions[] = array(
			'action'	=> $action,
			'target'	=> $target
		);
	}

    /**
     * Setea todas las acciones
     * @param  $action
     * @param bool $confirm
     * @return void
     */
	public function setActionAll($action, $confirm = true) {
		$this->actionsAll[] = array(
			'action'	=> $action,
			'confirm'	=> $confirm
		);
	}
	
	/**
     * Setea las acciones predeterminadas
     * @return void
     */
	private function setActions() { $this->assign('actions',$this->actions); }

    /**
     * Setea el Identificador
     * @return void
     */
	private function setId() { $this->assign('id',$this->idName); }

    /**
     * Setea las columnas Boleanas
     * @param string $column
     * @return void
     */
	public function setBooleanColumn($column='') { $this->booleanColumn[$column] = true; }

    /**
     * Añade columnas boleanas a la tabla
     * @param  $columns
     * @return void
     */
    public function addBooleanColumns( $columns ) { $this->booleanColumn = $columns; }
}

?>